/**
 * mysql单例
 * @author xialeistudio
 * @date 2016/5/9 0009
 */
'use strict';
const mysql = require('mysql')
		, Promise = require('bluebird')
		, Pool = require('mysql/lib/Pool')
		, Connection = require('mysql/lib/Connection')
		, configManager = require('./config')
		, loggerManager = require('./logger');
Promise.promisifyAll(Pool.prototype);
Promise.promisifyAll(Connection.prototype);
//创建连接池
const pool = mysql.createPool(configManager.loadConfig().mysql);
pool.on('connection', (connection)=> {
	loggerManager.info('[Mysql] connection ' + connection.threadId + ' initialized');
});
pool.on('enqueue', function() {
	loggerManager.warn('[Mysql] wait for connection');
});
/**
 * 从连接池获取连接
 */
module.exports = ()=> {
	return pool.getConnectionAsync();
};